package triggerhelper.errorChecks;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class ErrorCheck {
    
    private Component parent;
    private String name;
    private ArrayList<PossibleError> errorList;

    public ErrorCheck(String name_, Component parent_){
        name = name_;
        parent = parent_;
        errorList = new ArrayList<PossibleError>();
    }
    public ErrorCheck(ErrorCheck e){
        parent = e.getParent();
        name = e.getName();
        errorList = new ArrayList<PossibleError>(e.getErrorList());
    }

    public void addError(PossibleError e){
        errorList.add(e);
    }

    public boolean hasNoErrors(String input){
        boolean valid = true;
        if(input.isEmpty()){
            JOptionPane.showMessageDialog(parent, "I'm sorry, the " + name + " cannot be empty.");
            valid = false;
        }
        Iterator<PossibleError> i = errorList.iterator();
        while(valid && i.hasNext()){
            valid = i.next().check(input, name, parent);
        }
        return valid;
    }
    public boolean hasNoErrors(String input, Collection<String> noDuplicates){
        boolean valid = hasNoErrors(input);
        Iterator<String> check = noDuplicates.iterator();
        while (valid && check.hasNext()){
            if(input.equals(check.next())){
                JOptionPane.showMessageDialog(parent, "I'm sorry, the " + name + " contains a value which already exists.");
                valid = false;
            }
        }
        return valid;
    }

    public ArrayList<PossibleError> getErrorList() {
        return errorList;
    }
    public String getName() {
        return name;
    }
    public Component getParent() {
        return parent;
    }
}
